package com.example.tech34Base64;

import com.alibaba.fastjson.JSONObject;
import com.example.tech34Base64.utils.Base64Util;
import com.example.tech34Base64.utils.common.AssertUtil;
import com.example.tech34Base64.utils.common.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ReceiveService {

    private static Logger LOGGER = LoggerFactory.getLogger(ReceiveService.class);

    @Async
    public void receive(){
        //多线程获取远程文件
        try {
            String URL = "http://localhost:1001/send?msg=abc";
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("msg","我是receive,开始接受文件里");
            JSONObject reVal = HttpClientUtil.get(URL);
            //返回值为成功： { "code": 200,"msg": "监管系统同步点巡检离线版app缺陷：同步数据：成功：条数为：2 总条数为：2" }
            if (null != reVal) {
                handleFiles(reVal);
            } else {
                LOGGER.info("失败！");
            }
        }catch (Exception e){
            LOGGER.error("对外接口插入现场问题：报错："+e.getMessage(),e);
        }
    }

    public String handleFiles(JSONObject reVal){
        int num = 0;
        try {
            List<Map<String, Object>> list = (List<Map<String, Object>>) reVal.get("data");
            if (AssertUtil.isVal(list)) {
                for (int i = 0; i < list.size(); i++) {
                    Map<String, Object> map = list.get(i);
                    String name = (String) map.get("name");
                    LOGGER.info("待处理：名字为：" + name);
                    //附件处理：
                    List<String> files = (List<String>) map.get("files");
                    num+=files.size();
                    if (AssertUtil.isVal(files)) {
                        LOGGER.info("文件数为：" + files.size());
                        for (String fileStr : files) {
                            MultipartFile file = Base64Util.base64Convert(fileStr);//base64字符串格式为：UTF-8
                            LOGGER.info("文件名为：" + file.getName());
                            LOGGER.info("原始名为：" + file.getOriginalFilename());
                            LOGGER.info("大小为：" + file.getSize());
                            LOGGER.info("流对象为：" + file.getInputStream());
                            LOGGER.info("保存到：D:/test");
                            if (file.isEmpty()) {
                                continue;
                            }
                            //String fileName = file.getOriginalFilename();
                            File dest = new File(new File("D:/test").getAbsolutePath()+ "/" + name);
                            if (!dest.getParentFile().exists()) {
                                dest.getParentFile().mkdirs();
                            }
                            try {
                                file.transferTo(dest); // 保存文件
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("处理文件报错："+e.getMessage());
        }
        return "一共接收的文件数为：" + num;
    }
}
